import { ElMessageBox,ElMessage } from 'element-plus'
import Mock from "mockjs";
var userData = {}
var searchData = {}
if( localStorage.getItem('user_list') ){
    userData = JSON.parse( localStorage.getItem('user_list') )
}else{
    //生成数据
    userData = Mock.mock({
        'list|30':[
            {
                id:'@id',
                name:'@cname',
                email:'@email',
                'phone|13000000000-19999999999':1,
                rolename:'管理员',
                status:'@boolean'


            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('user_list',JSON.stringify(userData))
    
    
}
//查询接口
Mock.mock('/api/user/list','get',()=>{
    //返回数据
    return userData;
})
//搜索列表
if( localStorage.getItem('search_list') ){
    searchData = JSON.parse( localStorage.getItem('search_list') )
}else{
    //生成数据
    searchData = Mock.mock({
        list:[
            {
              
            }
        ]
    })
    //保存数据到本地存储
    localStorage.setItem('search_list',JSON.stringify(searchData))
    
    
}
// 创建接口
Mock.mock('/api/search/list','get',()=>{
    //返回数据
    return searchData;
})

//删除接口
Mock.mock('/api/user/detele','post',(req)=>{
    //接收参数,req.body是字符串格式，需要转为json对象
    var { id } = JSON.parse(req.body);
    //通过id，找到要删除的那条数据，执行删除
    var i = userData.list.findIndex((item)=>{
        return item.id == id;
        
    })
    // console.log(i);
    if( i != -1){
        userData.list.splice(i,1)
        localStorage.setItem('user_list',JSON.stringify(userData))
    }
    return {status:200,msg:'删除成功',list:userData.list}
})
//修改用户
Mock.mock('/api/user/edit','post',(req) =>{
    var body = JSON.parse(req.body)
    var id = body.id
    console.log(id);
    var i = userData.list.findIndex((item)=>{
        return item.id == id;
    })
    if(i != -1){
            userData.list[i].name = body.name,
            userData.list[i].phone = body.phone
            userData.list[i].email = body.email
            localStorage.setItem('user_list',JSON.stringify(userData))
            return {status:200,msg:'修改成功',list:userData.list}
        }else{
            return {status:404,msg:'修改失败',list:userData.list}
        }
    
})
    

//搜索用户
Mock.mock('api/user/search','post',(req)=>{
    var { phone } = JSON.parse(req.body);

    var i = userData.list.findIndex((item)=>{
        return item.phone == phone;
    })
    searchData.list = [];
    console.log(userData.list);
    if(i != -1){
        console.log(userData.list[i]);
        searchData.list.push(userData.list[i])
        console.log(searchData.list);
        
        return {status:200,msg:'搜索成功',list:searchData.list}
    }else{
        
        ElMessage({
            type: 'info',
            message: '查无此用户',
          })
          return {status:400,msg:'搜索失败',list:searchData.list}
    }
    // userData.list.map(function(item){
    //     if(item.indexOf(phone)>-1){
    //         searchData.list.push(item)
    //     }
    //     console.log(item);
    // })
})
//新增用户
Mock.mock('api/user/add','post',options =>{
    let body = JSON.parse(options.body)//获取请求参数
    let id = parseInt(body.id)
    let phone = parseInt(body.phone)
    let email = body.email
    let status = body.status
    let name = body.name
    let rolename = body.rolename
    let flag = true
    for(let item of userData.list){
        if(item.phone == phone) flag == false
    }
    if(flag){
        userData.list.push(
            {
                id:body.id,
                name:body.name,
                email:body.email,
                phone:body.phone,
                status:body.status,
                rolename:body.rolename
            }
        )
        localStorage.setItem('user_list',JSON.stringify(userData))
        return { status:200,msg:"添加成功",list:userData.list }
        
    }else{
        
        ElMessage({
            type: 'info',
            message: '电话已存在',
          })
          return { status:400,msg:"添加失败,电话号已存在" }
    }
    // localStorage.setItem('user_list',JSON.stringify(userData))
})